package com.pm.business.service.impl;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.pm.business.domain.*;
import com.pm.business.mapper.*;
import com.pm.common.core.domain.entity.SysUser;
import com.pm.common.utils.DateUtils;
import com.pm.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pm.business.service.IBidProjectProcessService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目投标执行Service业务层处理
 * 
 * @author zxl
 * @date 2022-05-12
 */
@Service
public class BidProjectProcessServiceImpl implements IBidProjectProcessService 
{
    @Autowired
    private BidProjectProcessMapper bidProjectProcessMapper;
    @Autowired
    private BidProjectMapper bidProjectMapper;
    @Autowired
    private BidProjectWorkflowPointMapper bidProjectWorkflowPointMapper;
    @Autowired
    private BidProjectPointMapper bidProjectPointMapper;
    @Autowired
    private BidProjectWorkflowMapper bidProjectWorkflowMapper;
    @Autowired
    private CurrentProjectMapper currentProjectMapper;
    /**
     * 查询项目投标执行
     * 
     * @param processId 项目投标执行主键
     * @return 项目投标执行
     */
    @Override
    public BidProjectProcess selectBidProjectProcessByProcessId(Long processId)
    {
        return bidProjectProcessMapper.selectBidProjectProcessByProcessId(processId);
    }
    @Override
    public List<Long> selectActiveBidProjectId(BidProjectProcess bidProjectProcess)
    {
        return bidProjectProcessMapper.selectActiveBidProjectId(bidProjectProcess);
    }

    /**
     * 查询项目投标执行列表
     * 
     * @param bidProjectProcess 项目投标执行
     * @return 项目投标执行
     */
    @Override
    public List<BidProjectProcess> selectBidProjectProcessList(BidProjectProcess bidProjectProcess)
    {
        return bidProjectProcessMapper.selectBidProjectProcessList(bidProjectProcess);
    }
    @Override
    public List<BidProjectProcess> selectBidProjectAndProcessList(List<Long> projectIdList)
    {
        return bidProjectProcessMapper.selectBidProjectAndProcessList(projectIdList);
    }
    @Override
    public List<BidProjectProcess> selectBidProjectAllPointList(List<Long> projectIdList)
    {
        return bidProjectProcessMapper.selectBidProjectAllPointList(projectIdList);
    }

    @Override
    public long selectMaxId()
    {
        return bidProjectProcessMapper.selectMaxId();
    }

    /**
     * 新增项目投标执行
     * 
     * @param bidProjectProcess 项目投标执行
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBidProjectProcess(BidProjectProcess bidProjectProcess)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        bidProjectProcess.setCreator(user.getUserName());

        if(bidProjectProcess.getExcuteTime()==null){
            bidProjectProcess.setCreateTime(DateUtils.getNowDate());
            bidProjectProcess.setExcuteTime(DateUtils.getNowDate());
        }else {
            bidProjectProcess.setCreateTime(bidProjectProcess.getExcuteTime());
        }


        BidProject bidProject = bidProjectMapper.selectBidProjectByProjectId(bidProjectProcess.getProjectId());
        BidProject updateBidProject = new BidProject();
        updateBidProject.setProjectId(bidProject.getProjectId());
        //执行 0 审核通过 1 审核不通过 -1
        if(bidProjectProcess.getStep()==0){
            //执行--- 执行不更新bidStep,只是把状态修改为待审核
            updateBidProject.setStepStatus("finish");//finish待审核
            //如果为中标状态，添加到当前项目
            /*if(bidProjectProcess.getBid()!=null && bidProjectProcess.getBid()){
                CurrentProject existsProject = currentProjectMapper.selectCurrentProjectByBidProjectId(bidProject.getProjectId());
                if(existsProject == null){
                    CurrentProject insertCurrentProject = new CurrentProject();
                    insertCurrentProject.setBidProjectId(bidProject.getProjectId());
                    insertCurrentProject.setProjectName(bidProject.getProjectName());
                    insertCurrentProject.setBidedTime(bidProjectProcess.getCreateTime());
                    insertCurrentProject.setBidAmount(new BigDecimal(0.00));
                    insertCurrentProject.setEstimatedBidTime(bidProjectProcess.getCreateTime());
                    insertCurrentProject.setWallArea(0.00);
                    insertCurrentProject.setConstructEnterprise(bidProject.getConstructionUnit());
                    insertCurrentProject.setProjectManager("暂无");
                    currentProjectMapper.insertCurrentProject(insertCurrentProject);
                    //人员备份
                    currentProjectMapper.copoyProjectPresonMap(insertCurrentProject.getProjectId(),bidProjectProcess.getProjectId());
                }
            }*/
        }else if(bidProjectProcess.getStep()==1) {
            //审核通过  更新bidStep,只是把状态修改为待执行
            bidProjectProcess.setExcuteTime(DateUtils.getNowDate());
            updateBidProject.setCurrentBidStep(bidProject.getCurrentBidStep()+1);
            updateBidProject.setStepStatus("process");//process待执行，也是默认状态
        }else if(bidProjectProcess.getStep()==-1) {
            //审核不通过
            //审核通过  不更新bidStep,只是把状态修改为待执行
            bidProjectProcess.setExcuteTime(DateUtils.getNowDate());
            updateBidProject.setStepStatus("error");
        }

        BidProjectPoint point = bidProjectPointMapper.selectBidProjectPointByBppId(bidProjectProcess.getPointId());
        if(point!=null){
            bidProjectProcess.setPointName(point.getPointName());
            if(bidProjectProcess.getStep()==-1){
                //审核不通过加后缀
                bidProjectProcess.setPointName(point.getPointName()+"审核不通过");
            }else if(bidProjectProcess.getStep()==1) {
                //审核通过  更新bidStep,只是把状态修改为待执行
                bidProjectProcess.setPointName(point.getPointName()+"审核通过");
            }

            bidProjectProcess.setPointOrder(point.getPointOrder());
        }
        /*List<Long> projectList = new ArrayList<Long>();
        projectList.add(updateBidProject.getProjectId());
        List<BidProjectProcess> pointList = bidProjectProcessMapper.selectBidProjectAllPointList(projectList);
        if(pointList!=null){
            String []points = pointList.get(0).getPointName().split(",");
            if(points!=null && points.length>=updateBidProject.getCurrentBidStep()){
                updateBidProject.setCurrentStepStr(points[updateBidProject.getCurrentBidStep()-1]);
            }
        }*/
        bidProjectMapper.updateBidProject(updateBidProject);

        return bidProjectProcessMapper.insertBidProjectProcess(bidProjectProcess);
    }

    /**
     * 更新project状态及activestep
     * 插入一个process
     * 备份唯一激活状态的工作流
     * @param bidProjectProcess
     * @return
     */
    @Override
    @Transactional
    public int activeBidProjectProcess(BidProjectProcess bidProjectProcess)
    {
        BidProject bidProject = bidProjectMapper.selectBidProjectByProjectId(bidProjectProcess.getProjectId());
        BidProject updateBidProject = new BidProject();
        updateBidProject.setProjectId(bidProject.getProjectId());

        //更新项目
        updateBidProject.setBidStatus("1");
        updateBidProject.setCurrentBidStep(0);
        updateBidProject.setStepStatus("finish");//finish待审核
        updateBidProject.setWorkflowId(bidProjectProcess.getWorkflowId());

        BidProjectWorkflow bidProjectWorkflow = bidProjectWorkflowMapper.selectBidProjectWorkflowByWorkflowId(bidProjectProcess.getWorkflowId());
        List<BidProjectWorkflowPoint>pointList = bidProjectWorkflow.getBidProjectWorkflowPointList();

        if(pointList!=null && pointList.size()>0){
            //insert 第一步的记录
            BidProjectProcess insertBidProjectProcess = new BidProjectProcess();
            SysUser user = SecurityUtils.getLoginUser().getUser();
            insertBidProjectProcess.setCreateTime(DateUtils.getNowDate());
            if(bidProjectProcess.getExcuteTime()==null){
                insertBidProjectProcess.setExcuteTime(DateUtils.getNowDate());
            }else {
                insertBidProjectProcess.setExcuteTime(bidProjectProcess.getExcuteTime());
            }
            insertBidProjectProcess.setCreator(user.getUserName());
            insertBidProjectProcess.setStep(0);
            insertBidProjectProcess.setPointName(pointList.get(0).getPointName());
            insertBidProjectProcess.setPointOrder(pointList.get(0).getPointOrder());
            insertBidProjectProcess.setPointId(pointList.get(0).getPointId());
            insertBidProjectProcess.setProjectId(bidProjectProcess.getProjectId());
            insertBidProjectProcess.setWorkflowId(bidProjectWorkflow.getWorkflowId());
            bidProjectProcessMapper.insertBidProjectProcess(insertBidProjectProcess);

            updateBidProject.setCurrentStepStr(pointList.get(0).getPointName());

            //备份工作流  查询工作流，工作流批量插入
            String workflowName = bidProjectWorkflow.getWorkflowName();
            List<BidProjectPoint> bidProjectPointList = new ArrayList<>();
            for (int i =0;i<pointList.size();i++) {
                BidProjectWorkflowPoint point = pointList.get(i);
                BidProjectPoint bidProjectPoint = new BidProjectPoint();
                bidProjectPoint.setProjectId(bidProjectProcess.getProjectId());
                bidProjectPoint.setPointName(point.getPointName());
                bidProjectPoint.setWorkflowName(workflowName);
                bidProjectPoint.setPointOrder(point.getPointOrder());
                if(i==0){
                    bidProjectPoint.setFilePath(bidProjectProcess.getFilePath());
                }

                bidProjectPointList.add(bidProjectPoint);
            }
            bidProjectPointMapper.batchInsertBidProjectPoint(bidProjectPointList);
        }
        bidProjectMapper.updateBidProject(updateBidProject);
        return 1;
    }

    /**
     * 修改项目投标执行
     * 
     * @param bidProjectProcess 项目投标执行
     * @return 结果
     */
    @Override
    public int updateBidProjectProcess(BidProjectProcess bidProjectProcess)
    {
        return bidProjectProcessMapper.updateBidProjectProcess(bidProjectProcess);
    }

    /**
     * 批量删除项目投标执行
     * 
     * @param processIds 需要删除的项目投标执行主键
     * @return 结果
     */
    @Override
    public int deleteBidProjectProcessByProcessIds(Long[] processIds)
    {
        return bidProjectProcessMapper.deleteBidProjectProcessByProcessIds(processIds);
    }

    /**
     * 删除项目投标执行信息
     * 
     * @param processId 项目投标执行主键
     * @return 结果
     */
    @Override
    public int deleteBidProjectProcessByProcessId(Long processId)
    {
        return bidProjectProcessMapper.deleteBidProjectProcessByProcessId(processId);
    }
}
